/* ------------------------------- command.js ------------------------------- */
VED.command = VED.event.extend({

   initialize: function( editor, commandDefinition ) {
      this.uiItems = [];

      this.exec = function( data ) {
         if( this.state == VED.TRISTATE_DISABLED )
            return false;

         if( this.editorFocus ) // Give editor focus if necessary (#4355).
         editor.focus();

         if ( this.fire( 'exec' ) === false )
            return true;

         return ( commandDefinition.exec.call( this, editor, data ) !== false );
      };

      this.refresh = function( editor, path ) {
         // Do nothing is we're on read-only and this command doesn't support it.
         // We don't need to disabled the command explicitely here, because this
         // is already done by the "readOnly" event listener.
         if ( !this.readOnly && editor.readOnly )
            return true;

         // Disable commands that are not allowed in the current selection path context.
         if ( this.context && !path.isContextFor( this.context ) ) {
            this.disable();
            return true;
         }

         // Make the "enabled" state as basis.
         this.enable();

         if ( this.fire( 'refresh', { editor: editor, path: path } ) === false )
            return true;

         return ( commandDefinition.refresh && commandDefinition.refresh.apply( this, arguments ) !== false );
      };

      VED.tools.extend( this, commandDefinition, {
         modes: { wysiwyg:1 },
         editorFocus: 1,
         contextSensitive: !!commandDefinition.context,
         state: VED.TRISTATE_OFF
      });

      // Call the VED.event constructor to initialize this instance.
      this.parent();
   },

   enable: function() {
      if ( this.state == VED.TRISTATE_DISABLED )
         this.setState( ( !this.preserveState || ( typeof this.previousState == 'undefined' ) ) ? VED.TRISTATE_OFF : this.previousState );
   },

   disable: function() {
      this.setState( VED.TRISTATE_DISABLED );
   },

   setState: function( newState ) {
      // Do nothing if there is no state change.
      if ( this.state == newState )
         return false;

      this.previousState = this.state;

      // Set the new state.
      this.state = newState;

      // Fire the "state" event, so other parts of the code can react to the
      // change.
      this.fire( 'state' );

      return true;
   },

   toggleState: function() {
      if ( this.state == VED.TRISTATE_OFF )
         this.setState( VED.TRISTATE_ON );
      else if ( this.state == VED.TRISTATE_ON )
         this.setState( VED.TRISTATE_OFF );
   }
});






